import data from "../../../../core/cache_data";
import { ReplacePageAni } from "../../../../core/game";
import Utils from "../../../../core/utils";
import CCCharacter from "../../../eggy/cc_game/cc_character";
import { GameUtils } from "../../../eggy/game_utils";
import CCCharacterBomb from "../../game/cc_character_bomb";
import { FightResult } from "../../game/fight";

const { ccclass, property } = cc._decorator;

@ccclass
export default class PageResult extends cc.Component {
    result: FightResult = null;
    @property(cc.Prefab)
    smoke: cc.Prefab = null;
    @property(cc.Node)
    root: cc.Node = null;
    ccChars: CCCharacterBomb[] = [];
    onLoad() {
        this.result = data.cache.get("result");

        let degDt = 360 / this.result.characters.length;
        let pt = cc.v2(440, 0);
        this.result.characters.forEach((charData, ind) => {
            let char = GameUtils.InstantiateCCGameCom("eggy_bomb", CCCharacterBomb);
            this.ccChars.push(char);
            char.setData(charData);
            pt = pt.rotate(cc.misc.degreesToRadians(degDt));
            char.node.setPosition(pt.x, pt.y / 3.5);
            this.root.addChild(char.node);
            char.node.zIndex = 1000 - char.node.y;
            if (this.result.winnerInd !== ind) {
                char.black();
                let smoke = cc.instantiate(this.smoke);
                smoke.setPosition(char.node.position);
                smoke.zIndex = 0;
                this.root.addChild(smoke);
            }
        });
    }
    _blocked = false;
    onClickReplay() {
        if (this._blocked) {
            return;
        }
        this._blocked = true;

        Utils.game.resReplacePage({
            name: "page_ready",
            ani: ReplacePageAni.Fade
        });
    }
}
